//
//  LKNavgationController.swift
//  weiBo
//
//  Created by Lukj on 2017/3/26.
//  Copyright © 2017年 lukj. All rights reserved.
//

import UIKit

class LKNavgationController: UINavigationController ,UIGestureRecognizerDelegate{

    override func viewDidLoad() {
        super.viewDidLoad()

        //  设置边缘手势
        self.interactivePopGestureRecognizer?.delegate = self
        
    }

    override func pushViewController(_ viewController: UIViewController, animated: Bool) {
        
        if self.viewControllers.count > 0 {
            if self.viewControllers.count == 1 {
                
                //  获取上一个目标控制器的标题
                let title = self.viewControllers.first?.title
                
                viewController.navigationItem.leftBarButtonItem = UIBarButtonItem(title: title!, imageName: "navigationbar_back_withtext",target: self, action: #selector(popAction))
                
                viewController.navigationItem.title = "当前是第\(self.viewControllers.count + 1)个控制器"
            }else {
                
                viewController.navigationItem.leftBarButtonItem = UIBarButtonItem(title: "返回", imageName: "navigationbar_back_withtext",target: self, action: #selector(popAction))
                
                viewController.navigationItem.title = "当前是第\(self.viewControllers.count + 1)个控制器"
                
            }
            
            //  隐藏tabbar,会有黑影
            //  1. 解决设置window背景色
            //  2. 设置tabbar视图的背景图片
            viewController.hidesBottomBarWhenPushed = true
            
        }
        
        
        print("当前控制器的个数\(self.viewControllers.count)")
        super.pushViewController(viewController, animated: true)
        print("当前控制器的个数\(self.viewControllers.count)")
    }
    
    func popAction() {
        
        self.popViewController(animated: true)
    }
    
    // MARK: ---UIGestureRecognizerDelegate
    func gestureRecognizerShouldBegin(_ gestureRecognizer: UIGestureRecognizer) -> Bool {
        
        if self.viewControllers.count == 1 {
            return false
        }
        return true
    }
    
    
    
}
